let mixin = {
  props: {},
  computed: {},
  data() {
    return {
      data() {
        return {
          websock: null,
          wsbaseurl: "ws://118.31.64.25:51925/daping/chart/testchartt"
        };
      },
      created() {
        //页面刚进入时开启长连接
        // this.initWebSocket()
      },
      destroyed: function() {
        //页面销毁时关闭长连接
        this.websocketclose();
      },
    };
  },
  mounted() {},
  methods: {
    initWebSocket() {
      //初始化weosocket
      // const wsuri = this.wsbaseurl; //ws地址
      const wsuri = "ws://118.31.64.25:51925/daping/chart/testchartt"

      this.websock = new WebSocket(wsuri);
      this.websock.onopen = this.websocketonopen;
      this.websock.onerror = this.websocketonerror;
      this.websock.onmessage = this.websocketonmessage;
      this.websock.onclose = this.websocketclose;
    },

    websocketonopen() {
      console.log("WebSocket连接成功");
    },
    websocketonerror(e) {
      //错误
      console.log("WebSocket连接发生错误");
    },
    websocketonmessage(e) {

      let received_msg = e.data;
      let received_msg_json = JSON.parse(received_msg)

      // console.log(received_msg_json);

      this.loadData(received_msg_json)

      // if (received_msg_json.topic) {
      //   switch (received_msg_json.topic) {
      //     case "point":

      //     default:
      //       break;
      //   }
      // }
    },

    websocketsend(agentData) {
      //数据发送
      this.websock.send(agentData);
    },

    websocketclose(e) {
      //关闭
      console.log("connection closed (" + e + ")");
    }
  }
}

export default mixin